jQuery(function($) {
	var doc = document,
		doc_ = $(doc);
	
	//错误信息
	function errorMessage(node, msg) {
		node.show().html(msg);
	}
	//清除错误信息
	function clearErrorMessage(node) {
		node.hide().html("");
	}
	
	//表单是否为空
	function empty(str) {
		if (str === "") {
			return true;
		}
		return false;
	}

	//验证所有信息
	var done = {
		proName: false,
		desc: false,
		cUrl: false,
		iUrl: false,
		aUrl: false,
		memName: false,
		jobTitle: false
	};
	
	
	
	/***验证项目名称***/
	function checkProName(elm) {
		var val = elm.val();
		
		if (empty(val)) {
			done.proName = false;
			errorMessage($(".c-e-1"), "项目名称不能为空!");
			return;
		}
		if (val.length > 20) {
			done.proName = false;
			errorMessage($(".c-e-1"), "项目名称字数请保持在20个字符以内!");
			return;
		}
		
		done.proName = true;
	}
	
	doc_.on("blur", ".c-i input", function() {
			checkProName($(this));
	});
	
	doc_.on("focus", ".c-i input", function() {
		clearErrorMessage($(".c-e-1"));
	});
	/***end***/
	
	
	/***验证一句话介绍***/
	
	function checkDesc(elm) {
		var val = elm.val();
		if (empty(val)) {
			done.desc = false;
			errorMessage($(".c-e-2"), "一句话介绍不能为空!");
			return;
		}
		if (val.length > 30) {
			done.desc = false;
			errorMessage($(".c-e-2"), "一句话介绍字数请保持在30个字符以内!");
			return;
		}
		done.desc = true;
	}
	
	doc_.on("blur", ".c-i textarea", function() {
		checkDesc($(this));
	});
	
	doc_.on("focus", ".c-i textarea", function() {
		clearErrorMessage($(".c-e-2"));
	});
	
		//更新字符长度
	doc_.on("keyup", ".c-i textarea", function(event) {
		var val = $(this).val(),
			len = val.length,
			keycode = event.keyCode;
		
		$(".re-num").html(len);
	});
	/***end***/

		//添加成员
	doc_.on("click", ".c-m-btn", function() {
			//添加到列表
			var member_name = $(".mem-name").val();
			var job_title = $(".job-title").val();

            $.ajax({
                type:"post",
                url:setProjectTeamByUserId,
                dataType:"json",
                data:{'name':member_name,'position':job_title},
                success:function(obj){
                    if(obj.level == "success") {
                        $(".c-m-t ul").append("<li pro_team_id="+obj.data['id']+"><em class='name'>" + obj.data['real_name'] + " - " + obj.data['position'] + "</em><button class='c-m-remove'>删除</button></li>");
                    }
                },
                error:function(obj){
                    var datajson = obj.responseJSON;
                    noty({type:datajson.level,text:datajson.message,timeout:600});
                }
            });


		//}
	});

		//删除成员列表
	function removeElement(parentNode) {
		setTimeout(function() {
			parentNode.remove();
		}, 500);
	}
	
	doc_.on("click", ".c-m-remove", function() {
		var close_btn = $(this),
			parentNode = close_btn.parent();
		var id = parentNode.attr('pro_team_id');
        $.ajax({
            type:"post",
            url:delProjectTeamById,
            dataType:"json",
            data:{'id':id},
            success:function(obj){
                if(obj.level == 'success'){
                    parentNode.addClass("fadeout");
                    //移除列表
                    removeElement(parentNode);
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({type:datajson.level,text:datajson.message,timeout:600});
            }
        });

	});

    jQuery.ajaxSetup({
        case:false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
	
	/***提交代码***/
	doc_.on("click", ".c-next button", function() {
		checkProName($(".c-i input"));
		checkDesc($(".c-i textarea"));
		
		//所以验证通过则跳转到 "需求页"
		if (done.proName && done.desc) {

            var projectName   = $('#projectName').val();
            var projectIntro   = $('#projectintro').val();
            var projectLogoId = $('#project_logo_id').val();
            var projectStatus = $('#projectStatus').val();
            var projectFields = $('#projectFields').val();
            var projectWebUrl = $('.c-url').val();
            var projectIosUrl = $('.i-url').val();
            var projectAndroidUrl = $('.a-url').val();

            var fields = [];
            $('input[type="checkbox"]:checked').each(function () {
                fields.push($(this).val());
            });

            var teamname = [];
            $('li[name="teamname"]').each(function(){
                teamname.push($(this).attr('pro_team_id'));
            })

            $.ajax({
                type: "post",
                url: createProject,
                dataType: "json",
                data: {'project_name':projectName,'project_intro':projectIntro,'img_projectlogo_id':projectLogoId,'project_status':projectStatus,"fields": fields,'web_url':projectWebUrl,'ios_url':projectIosUrl,'android_url':projectAndroidUrl,'teamname':teamname},
                success: function (obj) {
                    if(obj.level === "success"){
                        noty({text:obj.message,type:obj.level,
                            callback:{
                                afterClose:function(){
                                    window.location.href=cre_need;
                                }
                            }
                        });
                    }
                },
                error: function (obj) {
                    var datajson = obj.responseJSON;
                    noty({text:datajson.message,type:datajson.level,timeout:600});
                }
            });
        }
	});


});